<?php
	include_once("./dal/CountryDAL.php");
	include_once("./dal/UserDAL.php");
	if(!isLoggedIn())
	{
		header('Location: login.php');
	}
	
	$countries = getAllCountries();
	//print_r($countries);
	
	$updateUserID = "";
	$updateUsername = "";
	$updateFirstName = "";
	$updateLastName = "";
	$updateGender = "";
	$updateEmailAddress = "";
	
	$updateMailingAddressID = "";
	$updateStreet1 = "";
	$updateStreet2 = "";
	$updateCity = "";
	$updateCountry = "";
	
	$updateHomeContactID = "";
	$updateHomeContactNumber = "";
	
	$updateMobileContactID = "";
	$updateMobileContactNumber = "";
	
	$updateAboutMe = "";
	
	$fileUploadMessage = "";
	
	$newFileName = "";
	
	$formIsValid = true;
	
	$updateSuccess = null;
	$profileImgUploadSuccess = null;
	$profileUpdateAttempted = null;
	
	if(isset($_POST["btnUpdateProfile"]))
	{
		$profileUpdateAttempted = true;
		if($_POST["btnUpdateProfile"]=="Save")
		{		
		
			if(isset($_POST["txtUsername"]))
			{
				$updateUsername = trim($_POST["txtUsername"]);
			}
		
			if(isset($_POST["txtUserID"]))
			{
				$updateUserID = trim($_POST["txtUserID"]);
			}
			
			if(isset($_POST["txtFirstName"]))
			{
				$updateFirstName = trim($_POST["txtFirstName"]);
			}

			if(isset($_POST["txtLastName"]))
			{
				$updateLastName = trim($_POST["txtLastName"]);
			}		

			if(isset($_POST["sex"]))
			{
				$regGender = trim($_POST["sex"]);
			}	

			if(isset($_POST["txtEmailAddress"]))
			{
				$updateEmailAddress = trim($_POST["txtEmailAddress"]);
			}		
			
			if(isset($_POST["txtMailingAddressID"]))
			{
				$updateMailingAddressID = trim($_POST["txtMailingAddressID"]);
			}	
			
			if(isset($_POST["txtStreet1"]))
			{
				$updateStreet1 = trim($_POST["txtStreet1"]);
			}		

			if(isset($_POST["txtStreet2"]))
			{
				$updateStreet2 = trim($_POST["txtStreet2"]);
			}	
			
			if(isset($_POST["txtCity"]))
			{
				$updateCity = trim($_POST["txtCity"]);
			}	
			
			if(isset($_POST["ddlCountry"]))
			{
				$updateCountry = trim($_POST["ddlCountry"]);
			}

			if(isset($_POST["txtHomeContactID"]))
			{
				$updateHomeContactID = trim($_POST["txtHomeContactID"]);
			}	
			
			if(isset($_POST["txtHomeNumber"]))
			{
				$updateHomeContactNumber= trim($_POST["txtHomeNumber"]);
			}		

			if(isset($_POST["txtMobileContactID"]))
			{
				$updateMobileContactID = trim($_POST["txtMobileContactID"]);
			}	
			
			if(isset($_POST["txtMobileNumber"]))
			{
				$updateMobileContactNumber= trim($_POST["txtMobileNumber"]);
			}		

			if(isset($_POST["txtAboutme"]))
			{
				$updateAboutMe= trim($_POST["txtAboutme"]);
			}	

			if(isset($_POST["sex"]))
			{
				$updateGender= trim($_POST["sex"]);
			}				
			
			$validFile = true;
			$newFileName = "";
			
			//if a file was uploaded
			if($_FILES['fileProfileImg']['name'])
			{
				//if no errors...
				if(!$_FILES['fileProfileImg']['error'])
				{
					//now is the time to modify the future file name and validate the file
					$tempFileName = strtolower($_FILES['fileProfileImg']['tmp_name']); //rename file
					$newFileName = $_FILES['fileProfileImg']['name'];
					$newFileName = $updateUsername."_".$newFileName;
					
					if($_FILES['fileProfileImg']['size'] > (2048000)) //can't be larger than 2 MB
					{
						$validFile = false;
						$fileUploadMessage = 'Oops!  Your file\'s size is to large.';
					}
					
					//if the file has passed the test
					if($validFile)
					{
						//move it to where we want it to be
						$profileImgUploadSuccess = move_uploaded_file($_FILES['fileProfileImg']['tmp_name'], getcwd()."/img/profile/".$newFileName);
						
						$fileUploadMessage = 'Congratulations!  Your file was accepted.';
						if(!$profileImgUploadSuccess)
						{
							$formIsValid = false;
							$validFile = false;
						}
					}
				}
				//if there is an error...
				else
				{
					//set that to be the returned message
					$fileUploadMessage = 'Ooops!  Your upload triggered the following error:  '.$_FILES['fileProfileImg']['error'];
					$validFile = false;
				}
			}
			else
			{
				$validFile = false;
			}

			//print_r($countries);
						
			if($formIsValid)
			{
				$result = updateUser(
					$updateUserID,
					$updateFirstName,
					$updateLastName,
					$updateEmailAddress,
					$updateAboutMe,
					$updateGender,
					
					$updateMailingAddressID,
					$updateStreet1,
					$updateStreet2,
					$updateCountry,
					$updateCity,

					$updateHomeContactID,
					$updateHomeContactNumber,
					
					$updateMobileContactID,
					$updateMobileContactNumber,

					$newFileName
				);
				
				$updateSuccess = $result;
				
				if($updateSuccess==true)
				{	
					header(sprintf('Location: profile.php?id=%d&success',$updateUserID));
					exit;
				}
				else
				{
					header(sprintf('Location: profile.php?id=%d&failed',$updateUserID));	
					exit;
				}
			}
		}
	}
	
	function getContent()
	{
		global 
		$countries,
		$updateUsername,
		$updateUserID,
		$updateUsername,
		$updateFirstName,
		$updateLastName,
		$updateGender,
		$updateEmailAddress,
		$updateMailingAddressID,
		$updateStreet1,
		$updateStreet2,
		$updateCity,
		$updateCountry,
		$updateHomeContactID,
		$updateHomeContactNumber,
		$updateMobileContactID,
		$updateMobileContactNumber,
		$updateAboutMe,
		$newFileName,
		$formIsValid,
		$updateSuccess,
		$profileImgUploadSuccess,
		$profileUpdateAttempted;
		
		$updateProfileFailed = null;
		
		if(isset($_REQUEST["id"]))
		{
			
			//TODO: check that the request id is owned by the current logged in user if the user is not admin
			if((isset($profileUpdateAttempted) && $profileUpdateAttempted ===true && $updateSuccess ===true) || (!isset($profileUpdateAttempted)))
			{
				$profileID = $_REQUEST["id"];
				$userProfile = retrieveUserByID($profileID);
				if($userProfile!=null)
				{
					$updateUserID = $profileID;
					$updateUsername = $userProfile->username;
					$updateFirstName = $userProfile->firstName;
					$updateLastName = $userProfile->lastName;
					$updateGender = $userProfile->gender;
					$updateEmailAddress = $userProfile->emailAddress;
					
					$updateMailingAddressID = $userProfile->mailingAddress;
					$updateStreet1 = $userProfile->mailingStreet1;
					$updateStreet2 = $userProfile->mailingStreet1;
					$updateCity = $userProfile->mailingCity;
					$updateCountry = $userProfile->mailingCountry;
					
					
					$updateHomeContactID = $userProfile->homeContact;
					$updateHomeContactNumber = $userProfile->homeContactNumber;
					
					$updateMobileContactID = $userProfile->mobileContact;
					$updateMobileContactNumber = $userProfile->mobileContactNumber;
					
					$updateAboutMe = $userProfile->aboutMe;
					
					$newFileName = $userProfile->profileImgFilename;
					
					if(isset($_REQUEST["success"]))
					{
						$updateProfileFailed = false;
					}
					if(isset($_REQUEST["failed"]))
					{
						$updateProfileFailed = true;
					}
					include_once('./templates/EditProfileForm.php'); 
				}
				else
				{
					include_once('./templates/ProfileNotFound.php');
				}
			}
			else
			{
				$updateProfileFailed  = true;
				include_once('./templates/EditProfileForm.php'); 
			}
		}
		
	}
?>